/*
 * This file is part of ddesktop: remote desktop system for iPad 
 *
 * Copyright (C) 2010 Kazuhiro Satoh
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. if not, see <http://www.gnu.org/licenses/>.
 */


#import "SettingControl.h"
#import "HostInfoViewController.h"


@implementation HostInfoViewController


-(id) initWithAppStateManagerDelegate:(id)delegate setting:(Setting*)setting
{
	if ((self = [super initWithStyle:UITableViewStyleGrouped])) {
		_setting = setting;
		_hostId = -1;
		_hostInfoDir = nil;
		_appStateManagerDelegate = delegate;
		
		{ // init navigation button
			UIBarButtonItem* saveButton = [[UIBarButtonItem alloc] initWithTitle:@"Save"
																		  style:UIBarButtonItemStylePlain
																		 target:self
																		 action:@selector(navigationButtonSaveHostInfo:)];
			self.navigationItem.rightBarButtonItem = saveButton;
			[saveButton release];
			self.navigationItem.title = @"Host Info";
		}
	}
	return self;
}


- (void)dealloc{
	[super dealloc];
}


// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	SettingDirectory* dir = [_setting directoryForKeyPath:@"HostTemplate"];
	return [dir itemCount];
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	return nil;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	UITableViewCell *cell;
	
	cell = [tableView dequeueReusableCellWithIdentifier:@"CellWithTextField"];
	
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellWithTextField"] autorelease];
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
	}
	
	{
		SettingItem* item = [_hostInfoDir itemAtIndex:indexPath.row];
		
		int w, h, textFieldWidth;
		w = self.view.bounds.size.width;
		h = self.view.bounds.size.height;
		textFieldWidth = MIN(w, h) / 2.0;
		UIControl* control = [SettingControl controlForSettingItem:item withFrame:CGRectMake(0, 0, textFieldWidth, cell.bounds.size.height * 0.65)];
		cell.accessoryView = control;
				
		cell.textLabel.text = (NSString*)[item metaData:@"title"];
	}

	return cell;
}

/*
 To conform to Human Interface Guildelines, since selecting a row would have no effect (such as navigation), make sure that rows cannot be selected.
 */
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	return nil;
}



-(BOOL) loadHostInfoWithHostId:(int)hostId
{
	_hostId = hostId;
	
	if(_hostInfoDir != nil){
		[_hostInfoDir release];
	}
	
	if(_hostId == -1){ // new
		_hostInfoDir = [[_setting directoryForKeyPath:@"HostTemplate"] copy];
	}
	else{
		_hostInfoDir = [[[_setting directoryForKeyPath:@"HostList"] itemAtIndex:_hostId] copy];
	}
	
	[self.tableView reloadData];
	
	return true;
}


-(BOOL) newHostInfo
{
	return [self loadHostInfoWithHostId:-1];
}


// return false when failed to save
-(BOOL) saveToSettingObject;
{
	SettingDirectory* hostList = [_setting directoryForKeyPath:@"HostList"];
	
	if(_hostId == -1){
		[hostList addItemWithoutKey:_hostInfoDir];
	}
	else{
		[hostList replaceItemAtIndex:_hostId withItem:_hostInfoDir];
	}
	
	return true;
}
	

-(void)navigationButtonSaveHostInfo:(id)sender
{
	if([self saveToSettingObject]){
		[_appStateManagerDelegate popCurrentState];
	}
	else{
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Incomplate" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
		[alert show];
		[alert release];
	}
}



@end
